package com.kaifamiao.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;

@WebServlet( name = "general", value = {"/general"} )
// @WebServlet( value = {"/general"} )
// @WebServlet( "/general" )
public class GeneralServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.log( "[" + this + "]接收到"+ request.getMethod() +"请求" );
        // 设置从请求对象中获取数据时使用的编码
        request.setCharacterEncoding( "UTF-8" );
        // 设置向响应对象中输出数据时使用的编码
        response.setCharacterEncoding( "UTF-8" );

        // 从请求对象中读取数据
        String uname = request.getParameter( "username" );
        String passwd = request.getParameter( "password" );
        this.log( "用户名: " + uname + ", 密码: " + passwd );

        // 为响应对象设置MIME类型
        response.setContentType( "text/html;charset=UTF-8" );
        // 获得可以向响应对象缓冲区中输出数据的字符打印流
        PrintWriter out = response.getWriter();
        // 向响应对象缓冲区中打印字符数据
        out.println( "<h1>");
        out.println( "I am a GeneralServlet instance." );
        out.println( "</h1>");
        out.println( "<p>" + LocalDateTime.now() + "</p>");
    }

}
